﻿/// <summary>
/// This class is a usage example of some of the methods exposed by the LiveData class
/// Author: Shahal Hazan
/// </summary>
using System.Collections.Generic;
using LiveConnector;

namespace UsageExample
{
    /// <summary>
    /// A wrapper class for the LiveData class
    /// </summary>
    public class ContactsManager
    {
        /// <summary>
        /// Gets the contacts.
        /// </summary>
        /// <param name="locationId">The location id.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public List<Contact> GetContacts(string locationId, string token)
        {
            LiveContacts user;
            List<Contact> contacts = null;
            string error;

            LiveData.GetCollection<LiveContacts>(locationId, token, string.Empty, out user, out error);

            if (null != user && null != user.Contacts)
            {
                contacts = new List<Contact>(user.Contacts);
                contacts.Sort((contactA, contactB) => GetName(contactA).CompareTo(GetName(contactB)));
            }

            return contacts;
        }

        /// <summary>
        /// Gets the contact phones.
        /// </summary>
        /// <param name="locationId">The location id.</param>
        /// <param name="token">The token.</param>
        /// <param name="contactId">The contact id.</param>
        /// <returns></returns>
        public Phones GetContactPhones(string locationId, string token, string contactId)
        {
            Phones phones;
            string error;

            LiveData.GetCollection<Phones>(locationId, token, contactId, out phones, out error);

            return phones;
        }

        /// <summary>
        /// Gets the categories.
        /// </summary>
        /// <param name="locationId">The location id.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public Tags GetCategories(string locationId, string token)
        {
            Tags categories;
            string error;

            LiveData.GetCollection<Tags>(locationId, token, string.Empty, out categories, out error);

            return categories;
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <param name="contact">The contact.</param>
        /// <returns></returns>
        public static string GetName(Contact contact)
        {
            string name = null;

            if (null != contact)
            {
                if (null != contact.Profiles && null != contact.Profiles.Personal)
                {
                    if (!string.IsNullOrEmpty(contact.Profiles.Personal.FirstName) && !string.IsNullOrEmpty(contact.Profiles.Personal.LastName))
                    {
                        name = contact.Profiles.Personal.FirstName + " " + contact.Profiles.Personal.LastName;
                    }

                    name = name ?? contact.Profiles.Personal.DisplayName ?? contact.Profiles.Personal.NickName;
                }

                name = name ?? contact.WindowsLiveID;
            }
            return name;
        }

        /// <summary>
        /// Adds the contact.
        /// </summary>
        /// <param name="lid">The lid.</param>
        /// <param name="delegatedToken">The delegated token.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <param name="phoneNumber">The phone number.</param>
        /// <param name="phoneType">the phone.</param>
        /// <param name="isDefaultPhoneNumber">if set to <c>true</c> [is default phone number].</param>
        /// <param name="email">The email.</param>
        /// <param name="emailType">Type of the email.</param>
        /// <param name="isDefaultEmailAddress">if set to <c>true</c> [is default email address].</param>
        /// <param name="location">The location.</param>
        /// <returns></returns>
        public string AddContact(string lid, string delegatedToken,
            string firstName, string lastName,
            string phoneNumber, PhoneType phoneType, bool isDefaultPhoneNumber,
            string email, EmailType emailType, bool isDefaultEmailAddress, ref string location)
        {
            return LiveData.AddContact(lid, delegatedToken,
                                firstName, lastName,
                                phoneNumber, phoneType, isDefaultPhoneNumber,
                                email, emailType, isDefaultEmailAddress, ref location);
        }

        /// <summary>
        /// Edits the contact.
        /// </summary>
        /// <param name="lid">The lid.</param>
        /// <param name="delegatedToken">The delegated token.</param>
        /// <param name="contactId">The contact id.</param>
        /// <param name="phoneId">The phone id.</param>
        /// <param name="phoneNumber">The phone number.</param>
        /// <param name="phoneType"> the phone.</param>
        /// <param name="location">The location.</param>
        /// <returns></returns>
        public string EditContact(string lid, string delegatedToken,
            string contactId, string phoneId,
            string phoneNumber, PhoneType phoneType, ref string location)
        {
            string result = LiveData.DeleteContactPhone(lid, delegatedToken, contactId, phoneId);

            // If the user uses an empty number, we only delete the number in the update process
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                result = LiveData.AddContactPhone(lid, delegatedToken,
                                                  contactId, phoneNumber, phoneType, true, ref location);
            }

            return result;

        }



        /// <summary>
        /// Adds the contact to category.
        /// </summary>
        /// <param name="lid">The lid.</param>
        /// <param name="delegatedToken">The delegated token.</param>
        /// <param name="tagId">The tag id.</param>
        /// <param name="contactId">The contact id.</param>
        /// <param name="location">The location.</param>
        /// <returns></returns>
        public string AddContactToCategory(string lid, string delegatedToken,
                        string tagId, string contactId, ref string location)
        {
            return LiveData.AddContactToTag(lid, delegatedToken, tagId, contactId, ref location);
        }
    }
}
